﻿using Microsoft.EntityFrameworkCore;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using SmartParking.IService;
using SmartParking.Models;
using SmartParking.Models.EventAggregatorModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace SmartParking.ViewModels
{
    public class MainViewModel: BindableBase
    {
        IRegionManager _regionManager;
        IMenuService _menuService;

        private List<Entities.SysMenu> _originMenu;

        public MainViewModel(IDialogService dialogService,
            IMenuService menuService,
            IRegionManager regionManager,
            IEventAggregator eventAggregator)
        {
            _regionManager = regionManager;
            _menuService = menuService;

            dialogService.ShowDialog("LoginView",
                result =>
                {
                    if (result.Result != ButtonResult.OK)
                    {
                        System.Environment.Exit(0);
                    }
                });

            InitMenu();

            eventAggregator.GetEvent<RefreshMenuEvent>().Subscribe(() => 
            {
                InitMenu();
            });

            OpenViewCommand = new DelegateCommand<MenuItemModel>(DoOpenView);
        }

        private void InitMenu()
        {
            Menus.Clear();

            _originMenu = _menuService.GetMenuList("").ToList();
            FillMenus(Menus, 0);
        }

        #region 菜单功能

        private ObservableCollection<MenuItemModel> _menus = new ObservableCollection<MenuItemModel>();

        public ObservableCollection<MenuItemModel> Menus 
        {
            get { return _menus; }
            set 
            {
                SetProperty(ref _menus, value);
            }
        }

        private void FillMenus(ObservableCollection<MenuItemModel> models, int parentId) 
        {
            foreach (var item in _originMenu.Where(t => t.ParentId == parentId))
            {
                MenuItemModel model = new MenuItemModel
                {
                    MenuHeader = item.MenuHeader ?? "",
                    MenuIcon = item.MenuIcon ?? "",
                    TargetView = item.TargetView ?? ""
                };
                models.Add(model);

                FillMenus(model.Children, item.Id);
            }
        }

        
        public DelegateCommand<MenuItemModel> OpenViewCommand { get; set; }

        private void DoOpenView(MenuItemModel model) 
        {

            // 需要判断：双击的是父节点的时候，关闭或者打开；双击的是子节点，打开对应的页面
            if (model.Children != null && model.Children.Count > 0)
            {
                model.IsExpanded = !model.IsExpanded;
            }
            else if (!string.IsNullOrEmpty(model.TargetView))
            {
                //NavigationParameters nps = new NavigationParameters();
                //nps.Add("user", CurrentUser);
                //_regionManager.RequestNavigate("MainRegion", model.TargetView, nps);
                _regionManager.RequestNavigate("MainRegion", model.TargetView);
            }
        }
 

        #endregion
    }
}
